using UnityEngine;
using System.Collections.Generic;
using WS.Share;

namespace Share
{
    public class MapGrid : MonoBehaviour
    {
        [Header("Grid Settings")]
        public int gridSize = 1;
        public float gridSpacing = 1f;
        public Color gridColor = Color.gray;
        public Color gridColor2 = Color.gray;
        public Color gridColorZero = Color.gray;
        public float lineWidth = 0.05f;

        // 存储当前组件创建的LineRenderer列表
        private List<LineRenderer> createdLineRenderers = new List<LineRenderer>();

        private void OnEnable()
        {
            DrawGridLines();
        }

        private void OnDisable()
        {
            ClearGridLines();
        }

        /// <summary>
        /// 在Inspector中修改值时自动调用
        /// </summary>
        private void OnValidate()
        {
            // 如果组件已启用，则刷新网格
            if (isActiveAndEnabled)
            {
                RefreshGrid();
            }
        }

        /// <summary>
        /// 清除当前组件创建的所有网格线
        /// </summary>
        private void ClearGridLines()
        {
            // 释放所有当前组件创建的LineRenderer
            foreach (var lineRenderer in createdLineRenderers)
            {
                if (lineRenderer != null)
                {
                    LineRenderManager.Instance.ReleaseLine(lineRenderer);
                }
            }
            createdLineRenderers.Clear();
        }

        /// <summary>
        /// 刷新网格（清除并重新绘制）
        /// </summary>
        public void RefreshGrid()
        {
            ClearGridLines();
            DrawGridLines();
        }

        /// <summary>
        /// 绘制网格线
        /// </summary>
        private void DrawGridLines()
        {
            // 绘制X轴方向的网格线
            for (float x = -gridSize; x <= gridSize; x += gridSpacing)
            {
                var lineRenderer = LineRenderManager.Instance.GetLine();
                if (lineRenderer != null)
                {
                    // 添加到管理列表
                    createdLineRenderers.Add(lineRenderer);

                    // 设置LineRenderer属性
                    lineRenderer.startWidth = lineWidth;
                    lineRenderer.endWidth = lineWidth;
                    lineRenderer.positionCount = 2;
                    
                    // 设置线的位置
                    lineRenderer.SetPosition(0, new Vector3(x, -gridSize, 0));
                    lineRenderer.SetPosition(1, new Vector3(x, gridSize, 0));
                    
                    // 设置线的颜色
                    Color lineColor;
                    if (x == 0)
                    {
                        lineColor = gridColorZero;
                    }
                    else
                    {
                        lineColor = (x % 2 == 0) ? gridColor2 : gridColor;
                    }
                    
                    lineRenderer.startColor = lineColor;
                    lineRenderer.endColor = lineColor;
                }
            }

            // 绘制Z轴方向的网格线
            for (float z = -gridSize; z <= gridSize; z += gridSpacing)
            {
                var lineRenderer = LineRenderManager.Instance.GetLine();
                if (lineRenderer != null)
                {
                    // 添加到管理列表
                    createdLineRenderers.Add(lineRenderer);

                    // 设置LineRenderer属性
                    lineRenderer.startWidth = lineWidth;
                    lineRenderer.endWidth = lineWidth;
                    lineRenderer.positionCount = 2;
                    
                    // 设置线的位置
                    lineRenderer.SetPosition(0, new Vector3(-gridSize, z, 0));
                    lineRenderer.SetPosition(1, new Vector3(gridSize, z, 0));
                    
                    // 设置线的颜色
                    Color lineColor;
                    if (z == 0)
                    {
                        lineColor = gridColorZero;
                    }
                    else
                    {
                        lineColor = (z % 2 == 0) ? gridColor2 : gridColor;
                    }
                    
                    lineRenderer.startColor = lineColor;
                    lineRenderer.endColor = lineColor;
                }
            }
        }
    }
}
